
/* -----------------
* brief 
* 1. this is code gen by tools 
* 2. for add/update/delete/query the data save in sql database   
*/

package merchantCard

import (
	"fmt"
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
	"xorm.io/builder"

	//"github.com/mjiulee/lego/utils"
)

/* 根据id获取记录
 * -----------------
 */
func (t *MemberCardAccountLog) RecodeById(id int64) *MemberCardAccountLog {
	item := new(MemberCardAccountLog)
	lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 {
		return nil
	}
	return item
}

func (t *MemberCardAccountLog) RecodeByIdWithsession(session*xorm.Session, id int64) *MemberCardAccountLog {
	item := new(MemberCardAccountLog)
	session.ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 {
		return nil
	}
	return item
}

/* 添加
 * -----------------
 */
func (t *MemberCardAccountLog) AddRecode(item2add *MemberCardAccountLog) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

func (t *MemberCardAccountLog) AddRecodeWithsession(session*xorm.Session,item2add *MemberCardAccountLog) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}
	return true
}

/* 删除(软删除)
 * -----------------
 */
func (t *MemberCardAccountLog) SDelRecodeById(id int64) bool {
	item := new(MemberCardAccountLog)
	item.IfDel = 1
	_, err := lego.GetDBEngine().ID(id).Update(item)
	if err == nil {
		return true
	}
	return false
}

func (t *MemberCardAccountLog) SDelRecodeByIdWithSession(session*xorm.Session, id int64) bool {
	item := new(MemberCardAccountLog)
	item.IfDel = 1
	_, err := session.ID(id).Update(item)
	if err == nil {
		return true
	}
	return false
}

/* 删除(硬删除)*/
func (t *MemberCardAccountLog) DelRecodeById(id int64) bool {
	item := new(MemberCardAccountLog)
	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}

func (t *MemberCardAccountLog) DelRecodeByIdWithSession(session*xorm.Session,id int64) bool {
	item := new(MemberCardAccountLog)
	_, err := session.ID(id).Delete(item)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}

/* 更新
 * -----------------*/
func (t *MemberCardAccountLog) UpdateRecode(rc *MemberCardAccountLog) bool {
	_, err := lego.GetDBEngine().Id(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}

func (t *MemberCardAccountLog) UpdateRecodeWithSession(session *xorm.Session,rc *MemberCardAccountLog) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
        lego.LogError(err)
        return false
    }
    return true
}

func (t *MemberCardAccountLog) ListMemberCardAccountLogByUserCardId(userCardId int64, businessType []BusinessType,pageparam common.PageParam) ([]MemberCardAccountLog, int64, error) {
	page := pageparam.Page
	psize := pageparam.Psize

	start := (page-1*psize)
	resultList := make([]MemberCardAccountLog, 0)

	session := lego.GetDBEngine().Table(MemberCardAccountLog{}).Alias("t1").
		//Join("INNER", []string{TB_MCH_MEMBER_CARD, "t2"}, "t2.id = t1.mch_card_id").
		//Join("INNER", "tb_xx", "tb_xx.id = tb_xx.type_id").
		Where("t1.if_del = ?", 0).
		And("t1.user_card_id = ?", userCardId)

	businessType2 := make([]BusinessType, 0, len(businessType))
	for i, v := range businessType {
		if i > 30 { //防止外部输入的数据过大
			return nil, 0, fmt.Errorf("len of businessType list too long<%d>", len(businessType))
		}

		if v != BusinessType_NULL {
			//session = session.In("business_type", businessType)
			businessType2 = append(businessType2, v) //businessType为0时，表示搜索全部，所以不防止搜索条件里
		}
	}

	if len(businessType2) > 0 {
		session = session.Where(builder.In("business_type", businessType2))
	}

	//OrderBy("asc")

	//排序

	session2 := *session.Clone()
	err := session.Limit(psize, start).Find(&resultList)
	if err != nil {
		return nil, 0, err
	}

	total, err := session2.Count()
	if err != nil {
		return nil, 0, err
	}
	return resultList, total, err
}